package com.nuc.bigdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.bigdata.orm.entity.*;
import com.nuc.bigdata.orm.feign.CommodityDetailFeign;
import com.nuc.bigdata.orm.mapper.*;
import com.nuc.bigdata.service.GoodsDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @author JackLuo
 * @date 2019/11/29
 */
@Service
public class GoodsDetailsServiceImpl implements GoodsDetailsService {

    private final CommodityMapper goodsMapper;
    private final CommodityPictureMapper pictureMapper;
    private final CommoditySellMapper sellMapper;
    private final CommodityClicksMapper clicksMapper;
    private final CommodityViewPagerMapper viewPagerMapper;
    private final static Long LENGTH =6L;

    @Autowired
    GoodsDetailsServiceImpl(CommodityMapper goodsMapper,
                        CommodityPictureMapper pictureMapper,
                        CommoditySellMapper sellMapper,
                        CommodityClicksMapper clicksMapper,
                        CommodityViewPagerMapper viewPagerMapper) {
        this.goodsMapper = goodsMapper;
        this.pictureMapper = pictureMapper;
        this.sellMapper = sellMapper;
        this.clicksMapper = clicksMapper;
        this.viewPagerMapper = viewPagerMapper;
    }



    /**
     * 根据商品唯一标识获取商品的详细信息
     *
     * @param commodityId 商品唯一标识
     * @return 提供给front服务的商品Feign
     */
    @Override
    @Transactional(rollbackForClassName = "Exception")
    public CommodityDetailFeign queryByCommodityId(String commodityId) {
        Commodity commodity = goodsMapper.selectByCommodityId(commodityId);
        CommodityClicks clicks = selectClickByCommodityId(commodityId);
        if (ObjectUtils.isEmpty(clicks)){
            CommodityClicks click = new CommodityClicks();
            click.setClick(1L);
            click.setCommodityId(commodityId);
            clicksMapper.insert(click);
        }else {
            QueryWrapper<CommodityClicks> qw = new QueryWrapper<>();
            qw.eq("commodity_id",commodityId );
            Long click = clicks.getClick();
            clicks.setClick(click+1);
            Date date = new Date();
            clicks.setUpdateTime(date);
            clicksMapper.update(clicks,qw );
        }
        return transformCommodity(commodity);
    }

    /**
     * 通过商品唯一标识获取点击量
     * @param commodityId 商品唯一标识
     * @return 商品点击量实体对象
     */
    private CommodityClicks selectClickByCommodityId(String commodityId) {
        QueryWrapper<CommodityClicks> qw = new QueryWrapper<>();
        qw.eq("commodity_id",commodityId );
        return clicksMapper.selectOne(qw);
    }

    /**
     * 通过商品唯一标识获取商品详情的轮播图
     * @param commodityId 商品唯一标识
     * @return 包含图片id的ArrayList
     */
    private ArrayList<String> listViewPagerByCommodityId(String commodityId) {
        QueryWrapper<CommodityViewPager> qw = new QueryWrapper<>();
        qw.eq("commodity_id",commodityId );
        List<CommodityViewPager> commodityViewPagers = viewPagerMapper.selectList(qw);
        ArrayList<String> pictureIds = new ArrayList<>();
        commodityViewPagers.forEach(item -> pictureIds.add(item.getPictureId()));
        return pictureIds;
    }

    /**
     * 根据商品唯一标识获取图片介绍详情
     * @param commodityId 商品唯一标识
     * @return 商品详细图片的id的ArrayList
     */
    private ArrayList<String> listPictureDetailsByCommodityId(String commodityId) {
        QueryWrapper<CommodityPictureDetails> qw = new QueryWrapper<>();
        qw.eq("commodity_id",commodityId);
        List<CommodityPictureDetails> commodityPictureDetails = pictureMapper.selectList(qw);
        ArrayList<String> pictureIds = new ArrayList<>();
        commodityPictureDetails.forEach(item -> pictureIds.add(item.getPictureId() ));
        return pictureIds;
    }

    /**
     * 将商品实体转化为商品详情feign
     * @param goods 商品实体
     * @return 商品详情feign
     */
    private CommodityDetailFeign transformCommodity(Commodity goods) {
        CommodityDetailFeign detailFeign = new CommodityDetailFeign();
        String commodityId = goods.getCommodityId();
        // 获取商品上架信息
        CommoditySellDetails sellDetails = selectSellDetailsById(commodityId);
        // 获取商品轮播图
        ArrayList<String> viewPagers = listViewPagerByCommodityId(commodityId);
        // 获取图片介绍详情
        ArrayList<String> pictureDetails = listPictureDetailsByCommodityId(commodityId);
        detailFeign.setAbstractMsg(goods.getAbstractMsg());
        detailFeign.setAddTime(sellDetails.getCreateTime());
        detailFeign.setCommodityId(commodityId);
        detailFeign.setCommodityUom(sellDetails.getCommodityUom());
        detailFeign.setDescription(goods.getDescription());
        detailFeign.setPictureDetails(pictureDetails);
        detailFeign.setSellPrice(sellDetails.getSellPrice());
        detailFeign.setStickerPrice(goods.getStickerPrice());
        detailFeign.setStock(sellDetails.getStock());
        detailFeign.setViewPagers(viewPagers);
        return detailFeign;
    }

    /**
     * 根据商品唯一标识获取商品上架详情
     * @param commodityId 商品唯一标识
     * @return 商品上架详情实体对象
     */
    private CommoditySellDetails selectSellDetailsById(String commodityId) {
        QueryWrapper<CommoditySellDetails> qw = new QueryWrapper<>();
        qw.eq("commodity_id",commodityId );
        return sellMapper.selectOne(qw);
    }


}
